﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Transactions.*;
    import flash.geom.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import flash.utils.*;

    public class MechanicMapResource extends MapResource implements IMechanicUser {

        public const NOT_INITIALIZED_MECHANIC:String = "Data for this mechanic has not been initialized";
        public const STATE_STATIC:String = "static";

        protected var m_displayMechanics:Array;
        protected var m_actionMechanics:Dictionary;
        protected var m_hasActionMechanics:Boolean = false;
        protected var mechanicData:Dictionary;

        public function MechanicMapResource(_arg1:String){
            this.m_displayMechanics = [];
            this.m_actionMechanics = new Dictionary();
            this.mechanicData = new Dictionary();
            super(_arg1);
            setState(this.STATE_STATIC);
            this.initializeMechanics();
        }
        override public function loadObject(_arg1:Object):void{
            var _local2:String;
            super.loadObject(_arg1);
            if (_arg1.mechanicData){
                for (_local2 in _arg1.mechanicData) {
                    if (_arg1.mechanicData[_local2] != this.NOT_INITIALIZED_MECHANIC){
                        this.mechanicData[_local2] = _arg1.mechanicData[_local2];
                    };
                };
                dispatchEvent(new GenericObjectEvent(GenericObjectEvent.MECHANIC_DATA_CHANGED, "", true));
            };
        }
        override public function onBuildingConstructionCompleted_PostServerUpdate():void{
            super.onBuildingConstructionCompleted_PostServerUpdate();
            dispatchEvent(new GenericObjectEvent(GenericObjectEvent.MECHANIC_DATA_CHANGED, "", true));
        }
        override public function upgradeBuildingIfPossible(_arg1:Boolean=true, _arg2:Transaction=null, _arg3:Boolean=true):void{
            super.upgradeBuildingIfPossible(_arg1, _arg2, _arg3);
            this.initializeMechanics();
        }
        override public function onAddCrewMember():void{
            super.onAddCrewMember();
            dispatchEvent(new GenericObjectEvent(GenericObjectEvent.MECHANIC_DATA_CHANGED, "", true));
        }
        public function get displayMechanics():Array{
            return (this.m_displayMechanics);
        }
        public function get actionMechanics():Dictionary{
            return (this.m_actionMechanics);
        }
        public function get state():String{
            return (m_state);
        }
        override public function isSellable():Boolean{
            return (m_ownable);
        }
        protected function initializeMechanics():void{
            var _local2:XML;
            var _local3:XML;
            var _local4:MechanicConfigData;
            var _local5:XML;
            var _local6:MechanicConfigData;
            this.m_displayMechanics = [];
            this.m_actionMechanics = new Dictionary();
            this.m_hasActionMechanics = false;
            var _local1:XMLList = m_item.mechanicsXml;
            if (_local1){
                for each (_local2 in _local1.gameEventMechanics) {
                    for each (_local3 in _local2.mechanic) {
                        _local4 = new MechanicConfigData(_local3);
                        this.addActionMechanic(_local4, _local2.@gameMode);
                        this.m_hasActionMechanics = true;
                    };
                };
                if (((_local1) && ((_local1.clientDisplayMechanics.length() > 0)))){
                    for each (_local5 in _local1.clientDisplayMechanics.mechanic) {
                        _local6 = new MechanicConfigData(_local5);
                        this.addDisplayMechanic(_local6);
                    };
                };
            };
        }
        protected function addActionMechanic(_arg1:MechanicConfigData, _arg2:String):void{
            this.m_actionMechanics[_arg2] = this.addMechanic(this.m_actionMechanics[_arg2], _arg1);
        }
        protected function addDisplayMechanic(_arg1:MechanicConfigData):void{
            this.m_displayMechanics.push(MechanicManager.getInstance().createDisplayMechanicInstance(this, _arg1));
        }
        protected function addMechanic(_arg1:Array, _arg2:MechanicConfigData):Array{
            var _local3:Array = [];
            if (_arg1 != null){
                _local3 = _arg1;
            };
            _local3.push(_arg2);
            return (_local3);
        }
        public function getMechanicConfig(_arg1:String, _arg2:String="all"):MechanicConfigData{
            var _local4:String;
            var _local5:Boolean;
            var _local6:Array;
            var _local7:MechanicConfigData;
            var _local3:MechanicConfigData;
            for (_local4 in this.m_actionMechanics) {
                _local5 = (((_local4 == _arg2)) || ((_arg2 == "all")));
                if (((_local5) && (!((this.m_actionMechanics[_local4] == null))))){
                    _local6 = this.m_actionMechanics[_local4];
                    for each (_local7 in _local6) {
                        if (_local7.type == _arg1){
                            _local3 = _local7;
                            break;
                        };
                    };
                };
            };
            return (_local3);
        }
        public function getPrioritizedMechanicsForGameEvent(_arg1:String):Array{
            var _local3:Array;
            var _local4:MechanicConfigData;
            var _local2:Array = [];
            if (this.m_actionMechanics[_arg1] != null){
                _local3 = this.m_actionMechanics[_arg1];
                _local3.sortOn("priority", (Array.NUMERIC | Array.DESCENDING));
                for each (_local4 in _local3) {
                    _local2.push(_local4.type);
                };
                _local3 = null;
            };
            return (_local2);
        }
        public function getMechanicPriority(_arg1:String, _arg2:String):int{
            var _local4:Array;
            var _local5:MechanicConfigData;
            var _local3 = -1;
            if (this.m_actionMechanics[_arg2] != null){
                _local4 = this.m_actionMechanics[_arg2];
                for each (_local5 in _local4) {
                    if (_local5.type == _arg1){
                        _local3 = _local5.priority;
                        break;
                    };
                };
            };
            return (_local3);
        }
        public function getDataForMechanic(_arg1:String){
            var _local2:* = null;
            if (this.hasOwnProperty(_arg1)){
                _local2 = this[_arg1];
            } else {
                _local2 = this.mechanicData[_arg1];
            };
            return (_local2);
        }
        public function setDataForMechanic(_arg1:String, _arg2):void{
            if (this.hasOwnProperty(_arg1)){
                this[_arg1] = _arg2;
            } else {
                this.mechanicData[_arg1] = _arg2;
            };
            dispatchEvent(new GenericObjectEvent(GenericObjectEvent.MECHANIC_DATA_CHANGED, _arg1, true));
        }
        public function getMechanicData():Dictionary{
            return (this.mechanicData);
        }
        public function hasMechanicAvailable(_arg1:String):Boolean{
            return (!((this.getMechanicConfig(_arg1) == null)));
        }
        override public function onUpdate(_arg1:Number):void{
            var _local2:IClientGameMechanic;
            super.onUpdate(_arg1);
            for each (_local2 in this.m_displayMechanics) {
                _local2.updateDisplayObject(_arg1);
            };
        }
        override public function isVisitorInteractable():Boolean{
            var _local1:Boolean = super.isVisitorInteractable();
            return (((_local1) || (!((this.m_actionMechanics["GMVisit"] == null)))));
        }
        override public function isPixelInside(_arg1:Point):Boolean{
            var _local2:IClientGameMechanic;
            if (((m_displayObject) && (this.m_displayMechanics.length))){
                if (m_displayObject.getRect(m_displayObject.parent).containsPoint(_arg1)){
                    for each (_local2 in this.m_displayMechanics) {
                        if ((_local2 is IEdgeModifier)){
                            if ((_local2 as IEdgeModifier).isPixelInside(_arg1)){
                                return (true);
                            };
                        };
                    };
                };
            };
            return (super.isPixelInside(_arg1));
        }
        override public function getToolTipFloatOffset():int{
            var _local2:IClientGameMechanic;
            var _local1:int;
            for each (_local2 in this.m_displayMechanics) {
                if ((_local2 is IEdgeModifier)){
                    _local1 = (_local1 + (_local2 as IEdgeModifier).getFloatOffset());
                };
            };
            return ((super.getToolTipFloatOffset() + _local1));
        }
        override public function getToolTipStatus():String{
            var _local2:String;
            var _local3:String;
            var _local4:Array;
            var _local5:String;
            var _local6:IActionGameMechanic;
            var _local1:String = super.getToolTipStatus();
            if (this.m_hasActionMechanics){
                _local2 = getQualifiedClassName(Global.world.getTopGameMode());
                _local3 = _local2.split("::").pop();
                _local4 = (this.getPrioritizedMechanicsForGameEvent(_local3) as Array);
                for each (_local5 in _local4) {
                    _local6 = (MechanicManager.getInstance().getMechanicInstance(this, _local5, _local3) as IActionGameMechanic);
                    if ((_local6 is IToolTipModifier)){
                        if (_local6.hasOverrideForGameAction(_local3)){
                            _local1 = (_local6 as IToolTipModifier).getToolTipStatus();
                            break;
                        };
                    };
                };
            };
            return (_local1);
        }
        override public function getToolTipAction():String{
            var _local2:String;
            var _local3:String;
            var _local4:Array;
            var _local5:String;
            var _local6:IActionGameMechanic;
            var _local1:String = super.getToolTipAction();
            if (this.m_hasActionMechanics){
                _local2 = getQualifiedClassName(Global.world.getTopGameMode());
                _local3 = _local2.split("::").pop();
                _local4 = (this.getPrioritizedMechanicsForGameEvent(_local3) as Array);
                for each (_local5 in _local4) {
                    _local6 = (MechanicManager.getInstance().getMechanicInstance(this, _local5, _local3) as IActionGameMechanic);
                    if ((_local6 is IToolTipModifier)){
                        if (_local6.hasOverrideForGameAction(_local3)){
                            _local1 = (_local6 as IToolTipModifier).getToolTipAction();
                            break;
                        };
                    };
                };
            };
            return (_local1);
        }

    }
}//package Classes 
